package org.spiderflow.oss.function.extension;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.UrlAssert;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.spiderflow.executor.FunctionExtension;
import org.spiderflow.oss.model.Oss;
import org.spiderflow.oss.model.OssFile;
import org.spiderflow.oss.service.OssService;
import org.spiderflow.oss.utils.ExtractUtils;
import org.spiderflow.oss.utils.OSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Component
public class OssStringFunctionExtension implements FunctionExtension{
	
	private static OssService ossService;
	
	@Autowired
	private void setOssMapper(OssService ossService) {
		OssStringFunctionExtension.ossService = ossService;
	}

	@Override
	public Class<?> support() {
		return String.class;
	}
	
	public static OssFile ossUpload(String url,String ossId,String filePath) throws Exception {
		Oss oss = ossService.getOss(ossId);
		if(null != oss) {
			return OSSUtil.uploadFileToOss(oss, Jsoup.connect(url).ignoreContentType(true).execute().bodyAsBytes(), filePath);
		}
		return null;
	}

	public static String ossUploadAndReplace(String url,String ossId,String filePath) throws Exception {
		Oss oss = ossService.getOss(ossId);
		String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
		String suffix = fileName.substring(fileName.lastIndexOf(".") );
		fileName = UUID.randomUUID().toString()+suffix;
		if(null != oss) {
			return filePath+OSSUtil.uploadFileToOss(oss, Jsoup.connect(url).ignoreContentType(true).execute().bodyAsBytes(), filePath+fileName).getName();
		}
		return null;
	}
	//替换网页的图片未oss资源图片
	public static String ossUploadAndReplace(String html,String ossId,String filePath,String urlPrefix,String ossUrl,boolean dowmload) throws Exception {
		if(!dowmload){
			Document document = Jsoup.parse(html);
			html = document.html();
			return html;
		}
		Oss oss = ossService.getOss(ossId);
		Document document = Jsoup.parse(html);
		List<String> urlStr = (List<String>) ExtractUtils.selectors(html, "img", "attr", "src");
		if(CollectionUtils.isEmpty(urlStr)){
			return html;
		}
		document.select("img").last().remove();

		html = document.html();
		List<String> returnPics = new ArrayList<>();
		if(null != oss) {
			for (int i = 0; i < urlStr.size() ; i++) {
				String image = urlStr.get(i);
				String url = urlPrefix + image;
				String fileName = url.substring( url.lastIndexOf('/')+1, url.length() );
				String suffix = fileName.substring(fileName.lastIndexOf(".") );
				fileName = UUID.randomUUID().toString()+suffix;
				returnPics.add(ossUrl+filePath+OSSUtil.uploadFileToOss(oss, Jsoup.connect(url).ignoreContentType(true).execute().bodyAsBytes(), filePath+fileName).getName());
			}
			//替换图片
			String str = html;
			String cont = html;
			for(int k = 0; k < returnPics.size(); k ++){
				int i = str.indexOf("src=\"");
				if (i != -1) {
					str = str.substring(i+5, str.length());
					int j = str.indexOf("\"");
					String pic_url = str.substring(0, j);
					String pic_path = returnPics.get(k);
					if(StringUtils.isNotEmpty(pic_url) && StringUtils.isNotEmpty(pic_path)){
						cont = cont.replace(pic_url, pic_path);
						str = str.substring(j,str.length());
					}
				} else{
					break;
				}
			}
			return cont;

		}

		return html;
	}


	public static void ossDelete(String filePath,String ossId) {
		OSSUtil.deleteFile(ossService.getOss(ossId), filePath);
	}

}
